<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/1.jpg" alt />
        <span>电商后台管理系统</span>
      </div>
   
      <el-button type="info" @click="logoout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="collapse ?'64px':'200px'">
        <div  class="Topcollapse" @click="collapses" > ||| </div>
        <el-menu   default-active="2" class="el-menu-vertical-demo"  background-color="#333744" text-color="#fff" active-text-color="hotpink"  unique-opened :collapse="collapse"  :collapse-transition="false"
        router :default-active="activePath"
        >
        <!-- unique-opened 就是只能打开一个菜单 -->
          <el-submenu :index="item.id+''" :key="item.id" v-for="(item , index)  in menuList">
            <template slot="title">
              <i class="iconfont"  :class="iconList[index]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <!-- <span @click="saveName('/'+subItem.path)">{{subItem.authName}}</span> -->
                <span >{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
<router-view></router-view>


      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      collapse:false,
      /////创建了一个空数组 就是我希望把获取到的数据放到这个空数组里面，上面可以直接调用data里面的东西
      menuList: [],
      activePath:'',
      iconList:["icon-user","icon-users","icon-baobiao","icon-lock_fill","icon-danju"]
    };
  },
  created() {
      ////这个是最早可以访问到data属性与methods方法的钩子函数 , 
    this.getMenuList();
    this.activePath=this.$route.path
  
    // this.activePath=window.sessionStorage.getItem("activePath")
    /////下面这种方法有问题 , 上面这种 是现在完美的解决 是根据url参数的
  },
  watch: {
    "$route.path":function(newValue){
      this.activePath=newValue
    }
  },
  methods: {
    logoout() {
      window.sessionStorage.clear();
      this.$router.push("/");
    },
   async getMenuList(){
     ///////发送一个get请求  通过解构赋值把data的数据放在res中
        const {data:res}=await this.$http.get("menus");
        // console.log(res);把
        if(!res) return this.$message.error(res.meta.msg)
        this.menuList= res.data
        // console.log(this.menuList);
         
    },
    collapses(){
      this.collapse=!this.collapse
    },
    saveName(activePath){
      // window.sessionStorage.setItem('activePath',activePath);
      // this.activePath=activePath
    }
  }
};
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
// 所有的都是类
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
    img {
      width: 60px;
    }
  }
}
.el-aside {
  background: #333744;
  .el-menu {
    border-right:none;
  }
}
.el-main {
  background: #eaedf1;
}
.iconfont {
  margin-right :8px;
 
}
.Topcollapse {
  text-align: center;
  height: 24px;
  line-height: 24px;
  letter-spacing: 0.2em;
  cursor: pointer;
  

}
</style>